<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  activity_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-17.
//  Copyright 2011 lishuzu. All rights reserved.
// 
class Activity_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->category = $this->config->item('category');
			$this->fromsource = $this->config->item('fromsource');
		}
		// 得到活动数量
		function get_act_count($isindex=0){
			if ($isindex) {
				$this->db->where('endtime >=', now());
				$this->db->where('status >=', 0);
			}
			return $this->db->count_all_results('activity');
		}
		
		// 得到活动数据
		function get_activitys($num = '', $offset = '', $isindex=''){
			if ($isindex) {
				$this->db->order_by("displayorder", "DESC");
				$this->db->order_by('starttime', 'DESC');
				$this->db->where('endtime >=', now());
			}

			$this->db->order_by('dateline', 'DESC');
			//不查询下架和删除的活动
			$this->db->where('status >=', 0);
			if (empty($num) || $offset ==='') {
				$this->db->limit('100');
				$query = $this->db->get("activity");	
			}else{
				$query = $this->db->get("activity", $num, $offset);				
			}
			
			foreach ($query->result_array() as $row) {
				//图片模式切换 0 外链 1上传
				if ($row['img_mode'] == 1) {
					$row['act_img'] = base_url().'upload/attachments/'.$row['uploadimg'];
				}else{
					$row['act_img'] = $row['imageurl'];					
				}
				$row['fromname'] = $row['fromid'] ? $this->fromsource[$row['fromid']]['name'] : '未知';
				$row['catname'] = $row['catid'] ? $this->category[$row['catid']]['name'] : '未分类';
				$activitys[] = $row;
			}
			return $activitys;
		}
		
		// 得到活动数据
		function get_activitys_by_catid($catid, $num = '', $offset = ''){
			
			$this->db->order_by("displayorder", "DESC");
			$this->db->order_by('starttime', 'DESC');
			$this->db->where('endtime >=', now());
			$this->db->where('catid', $catid);
			$this->db->order_by('dateline', 'DESC');
			//不查询下架和删除的活动
			$this->db->where('status >=', 0);
			if (empty($num) || $offset ==='') {
				$this->db->limit('100');
				$query = $this->db->get("activity");	
			}else{
				$query = $this->db->get("activity", $num, $offset);				
			}
			
			foreach ($query->result_array() as $row) {
				//图片模式切换 0 外链 1上传
				if ($row['img_mode'] == 1) {
					$row['act_img'] = base_url().'upload/attachments/'.$row['uploadimg'];
				}else{
					$row['act_img'] = $row['imageurl'];					
				}
				$row['fromname'] = $row['fromid'] ? $this->fromsource[$row['fromid']]['name'] : '未知';
				$row['catname'] = $row['catid'] ? $this->category[$row['catid']]['name'] : '未分类';
				$activitys[] = $row;
			}
			return $activitys;
		}

		//得到下架活动
		function get_downindex_activitys($num = '', $offset = '', $isindex=''){
			if ($isindex) {
				$this->db->order_by("displayorder", "DESC");
				$this->db->order_by('starttime', 'DESC');
			}

			$this->db->order_by('dateline', 'DESC');
			//不查询下架和删除的活动
			$this->db->where('status =', -2);
			if (empty($num) || $offset ==='') {
				$this->db->limit('100');
				$query = $this->db->get("activity");	
			}else{
				$query = $this->db->get("activity", $num, $offset);				
			}
			
			foreach ($query->result_array() as $row) {
				//图片模式切换 0 外链 1上传
				if ($row['img_mode'] == 1) {
					$row['act_img'] = base_url().'upload/attachments/'.$row['uploadimg'];
				}else{
					$row['act_img'] = $row['imageurl'];					
				}
				$row['fromname'] = $row['fromid'] ? $this->fromsource[$row['fromid']]['name'] : '未知';
				$row['catname'] = $row['catid'] ? $this->category[$row['catid']]['name'] : '未分类';
				$activitys[] = $row;
			}
			return $activitys;
		}
		
		function get_activitys_by_aids($aids = array()){
			if (is_array($aids)) {
				//收藏可以查询到下架的产品
				$this->db->where('status !=', '-1');				
				$this->db->order_by("displayorder", "DESC");
				$this->db->order_by('starttime', 'DESC');
				$this->db->order_by('dateline', 'DESC');
				$this->db->where_in('aid', $aids);
				$query = $this->db->get('activity');
				foreach ($query->result_array() as $row) {
					//图片模式切换 0 外链 1上传
					if ($row['img_mode'] == 1) {
						$row['act_img'] = base_url().'upload/attachments/'.$row['uploadimg'];
					}else{
						$row['act_img'] = $row['imageurl'];					
					}
					$row['fromname'] = $row['fromid'] ? $this->fromsource[$row['fromid']]['name'] : '未知';
					$row['catname'] = $row['catid'] ? $this->category[$row['catid']]['name'] : '未分类';
					$activitys[] = $row;
				}
				return $activitys;
			}
			return false;
		}
		
		function get_activity_by_aid($aid){
			if ($aid) {
				$this->db->where('aid', $aid);
				$query = $this->db->get('activity');
				$activity = $query->row_array();
				//图片模式切换 0 外链 1上传
				if ($activity['img_mode'] == 1) {
					$activity['act_img'] = base_url().'upload/attachments/'.$activity['uploadimg'];
				}else{
					$activity['act_img'] = $activity['imageurl'];					
				}
				$activity['fromname'] = $activity['fromid'] ? $this->fromsource[$activity['fromid']]['name'] : '未知';
				$activity['catname'] = $activity['catid'] ? $this->category[$activity['catid']]['name'] : '未分类';
				return $activity;
			}
			return false;
		}
		
		function insert_activity($data){
			if ($data) {
				$this->db->insert('activity', $data);
				return $this->db->insert_id();
			}
			return false;
		}
		
		function update_activity($data){
			if (!empty($data['aid'])) {
				$this->db->where('aid', $data['aid']);
				unset($data['aid']);
				$this->db->update('activity', $data);
				return $this->db->affected_rows();
			}
			return false;
		}
		
		function delete_activitys($ids){
			if($ids){
				foreach ($ids as $id) {
					$this->db->where('aid', $id);
					$num = $this->db->count_all_results('goods');
					if ($num) {
						$undel[] = $id;
					}else{
						$this->db->where('aid', $id);
						$query = $this->db->get('activity');
						$act = $query->row_array();
						if ($act['fromid'] && $act['fromaid']) {
							$website = $this->fromsource[$act['fromid']];
							if ($website) {
								$this->db->where('aid', $act['fromaid']);
								$this->db->update($website['activity_table'], array('status'=>'0'));
							}
						}
						$this->db->delete('activity', array('aid' => $id)); 
					}
				}
				return $undel;
			}
			return false;
		}
		//批量下架
		function downindexs($ids){
			if($ids){
				$this->db->where_in('aid', $ids);
				$this->db->limit(COUNT($ids));
				$this->db->update('activity', array("status" => "-2"));
				if($this->db->affected_rows() <= 0){
					return false;
				}
				return true;
			}
			return false;
		}
		
		//批量上架
		function upindexs($ids){
			if($ids){
				$this->db->where_in('aid', $ids);
				$this->db->limit(COUNT($ids));
				$this->db->update('activity', array("status" => "0"));
				if($this->db->affected_rows() <= 0){
					return false;
				}
				return true;
			}
			return false;
		}
}
?>